package main

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"strconv"
)

type Song struct {
	Album          string
	Picture        string
	Ssid           string
	Artist         string
	Url            string
	Company        string
	Title          string
	RatingAvg      float32 `json:"rating_avg"`
	Length         int64
	Subtype        string
	PublicTime     string `json:public_time`
	SongListsCount int64  `json:"songlists_count"`
	Sid            string
	Aid            string
	Sha256         string
	Kbps           string
	Albumtitle     string
	Like           int64
}

type SongList []Song

type Channel struct {
	NameEn    string `json:"name_en"`
	SeqId     int64  `json:"seq_id"`
	AbbrEn    string `json:"abbr_en"`
	Name      string
	ChannelId interface{} `json:"channel_id"`
}

type ChannelList []Channel

type FmPeople struct {
	R                   int64
	Version_max         int64
	Is_show_quick_start int64 `json:"is_show_quick_start"`
	Song                SongList
}

type FmChannel struct {
	Channels ChannelList
}

type FmErr struct {
	Err string
	R   string
}

type FmLogin struct {
	UserId   string `json:"user_id"`
	Err      string
	Token    string
	Expire   string
	R        int64
	UserName string `json:"user_name"`
	Email    string
}

type Fm struct {
	IsLogin  bool
	Login    FmLogin
	Channels FmChannel
	People   FmPeople
	PlayList []Song
	Histroy  []Song
	CurCh    int64
	Song     Song
}

func (f *Fm) OauthLogin(account string, pwd string) {
	v := url.Values{}
	v.Set("email", account)
	v.Set("password", pwd)
	v.Set("app_name", "radio_desktop_win")
	v.Set("version", "100")

	resp, err := http.PostForm("http://www.douban.com/j/app/login", v)

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Println(err.Error())
	}
	err = json.Unmarshal(body, &f.Login)
	if err != nil {
		log.Println(err.Error())
	} else {
		if f.Login.R == 0 {
			f.IsLogin = true
		}
	}
}

func (f *Fm) GetCh() {
	resp, err := http.Get("http://www.douban.com/j/app/radio/channels?app_name=radio_desktop_win&version=100&type=n")
	if err != nil {
		log.Println(err.Error())
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Println(err.Error())
	}
	err = json.Unmarshal(body, &f.Channels)
	if err != nil {
		log.Println(err.Error())
	}
}

func (f *Fm) GetPeople(n int64) {

	pUrl := "http://www.douban.com/j/app/radio/people?app_name=radio_desktop_win&version=100&type=n&channel=" + strconv.FormatInt(n, 10)

	if f.IsLogin {
		log.Println("Login UserName is " + f.Login.UserName)
		pUrl = pUrl + "&user_id=" + f.Login.UserId + "&token=" + f.Login.Token + "&expire=" + f.Login.Expire
	}

	resp, err := http.Get(pUrl)
	if err != nil {
		log.Println(err.Error())
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Println(err.Error())
	}
	err = json.Unmarshal(body, &f.People)
	if err != nil {
		log.Println(err.Error())
	} else {
		if f.People.R == 0 {
			f.CurCh = n
			f.PlayList = f.People.Song
		}
	}
}
